package class02;

public class Code02_DeleteGivenValue {

    public static class Node {
        public int value;
        public Node next;

        public Node(int data) {
            this.value = data;
        }
    }

    //单链表, 值为指定num的节点
    public static Node removeValue(Node head, int num) {
        while (head != null) {
            if (head.value != num) {
                break;
            }
            //跳至下一个节点
            head = head.next;
        }
        // head来到 第一个不需要删的位置(避免头部均=num)
        Node pre = head;
        Node cur = head;
        //确定好需要返回的头部后, 遍历后续节点并删除=num的节点
        while (cur != null) {
            if (cur.value == num) {
                //指向下下个节点
                pre.next = cur.next;
            } else {
                pre = cur;
            }
            cur = cur.next;
        }
        return head;
    }

}
